using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.ObjectModel;
//using Db4objects;
using IronPython;
using Microsoft.Scripting;
namespace NCS_CS
{
    static class ModMain
    {
        [DllImport("winmm.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern int timeGetTime();
        //public static bool isshoudong;
        //public static bool isyuchuli;
        public static bool buChuCO2;
        //public static bool isAddSample;
        public static Mcsy.csyState csystate;
        public const int powerChannel = 8;//9111 0通道
        public const int NzeroChannel = 4;//6208 4通道
        public static CTran gtran;
        public static NCS.program program1;
        public static NCS.curveXishu cxishu;
        public static Microsoft.Scripting.Hosting.ScriptEngine m_python;
        public static Microsoft.Scripting.Hosting.ScriptScope gscope;
        public static int nvlv;
        public static int[] vlV;
        public static string[] vlvName;
        private static ISession session;
        public const int NUMChanel = 4;
        public const bool firstKoujixian = true;
        public static bool busy;
        //public static bool SYNC;
        public static DemoDbNew gdemo;
        public static IList gchanel;
        public static System.Collections.Generic.Dictionary<string, NCS.chanel > gchanelDic;
        public static DBdata gDb;//数据库
        public static bool makemethod;
        public static NCS.muser user1;
        public static NCS.method method1;
        private static frmMain gfrmMain;
        private static FrmMethod gfrmMethod;
        private static frmResult gfrmResult;
        static bool frmMain_isCreate;
        static bool frmResult_isCreate;
        static bool frmMethod_isCreate;
        public static frmMain getFrmMain()
        {
            return GetForm<frmMain>(ref gfrmMain, ref frmMain_isCreate);
        }
        public static frmResult getFrmResult()
        {
            return GetForm<frmResult>(ref gfrmResult, ref frmResult_isCreate);
        }
        public static FrmMethod getFrmMethod()
        {
            return GetForm<FrmMethod>(ref gfrmMethod,ref frmMethod_isCreate);
        }
        [DebuggerStepThrough]
        static T GetForm<T>(ref T instance, ref bool isCreating) where T : Form, new()
        {
            if (instance == null || instance.IsDisposed)
            {
                if (isCreating)
                {
                    throw new InvalidOperationException("WinForms_RecursiveFormCreate");
                }
                isCreating = true;
                try
                {
                    instance = new T();
                }
                catch (System.Reflection.TargetInvocationException ex)
                {
                    throw new InvalidOperationException("WinForms_SeeInnerException");
                }
                finally
                {
                    isCreating = false;
                }
            }
            return instance;
        }
        [STAThread]
        public static void Main()
        {
            //Form1 f = new Form1();
            //f.ShowDialog();
            //return;
            beginProgram();
            gfrmResult = new frmResult();
            modLog.savelogmsg("init path:" + modLog.initpath);
            if (program1.demo)
            {
                gdemo = new DemoDbNew();
            }
            gfrmMain = new frmMain();
            gscope.SetVariable("frmMain", gfrmMain);
            //gfrmMain.Show();
            Application.Run(gfrmMain);
        }
        public static ISession getsession()
        {
            if (session!=null )
            {
                if (session.IsOpen)
                {
                    return session;
                }
                else
                {
                    Configuration c = NCS.Class1.configuration;
                    ISessionFactory factory = c.BuildSessionFactory();
                    session = factory.OpenSession();
                    return session;
                }
            }
            else
            {
                Configuration c = NCS.Class1.configuration;
                ISessionFactory factory = c.BuildSessionFactory();
                session=factory.OpenSession();
                return session;
            }
        }
        public static void beginProgram()
        {
            try
            {
                //ModMain.csystate = Mcsy.csyState.enmReady;
                buChuCO2 = false;
                modLog.initpath =Application.StartupPath ;// NCS_CS.My.MyProject.Application.Info.DirectoryPath;
                Configuration c = NCS.Class1.configuration;
                ISessionFactory factory = c.BuildSessionFactory();
                session = factory.OpenSession();
                gDb = new DBdata();
                gtran = new CTran();
                gDb.getProgram1();
                //DEMO = gDb.getdemo();
                gDb.myLoadEleChanel();
                user1 = null;
                method1 = null;
                m_python = IronPython.Hosting.Python.CreateEngine();
                System.Collections.Generic.ICollection<string> paths= m_python.GetSearchPaths();
                paths.Add(modLog.initpath);
                //m_python.SetSearchPaths( modLog.initpath);
                m_python.SetSearchPaths(paths);
                //Microsoft.Scripting.Hosting.ScriptScope 
                gscope= m_python.CreateScope();
                gscope.SetVariable("session", session);
                //m_python.Globals["session"] = session;
                logon();
                //gDb.saveUseHistory();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString(), "");
            }
        }

        public static void endProgram()
        {
            ////gDb.saveUseHistoryEndTime();
            ModMain.getsession().Flush();
            gDb = null;
            gchanel = null;
            System.Environment.Exit(0);
        }
        public static string getFirstWord(string s)
        {
            string functionReturnValue = null;
            functionReturnValue = "";
            string a = null;
            int n = 0;
            int i = 0;
            n = s.Length;
            i = 1;
            while (i <= n)
            {
                a = s.Substring(i, 1);//Strings.Mid(s, i, 1);
                if (a == " ")
                    break; // TODO: might not be correct. Was : Exit Do
                i = i + 1;
            }
            if (i - 1 >= 1)
            {
                functionReturnValue = s.Substring(1, i - 1);// Strings.Mid(s, 1, i - 1);
            }
            return functionReturnValue;
        }
        public static void logon()
        {
            frmLogin l = new frmLogin();
            l.ShowDialog();
            if (!l.LoginSucceeded)
            {
                System.Environment.Exit(0);
            }
            l.Close();
        }

        public static void setDynaArr(ref double[] y, int num, double v)
        {
            int n = 0;
            n = y.Length;//Information.UBound(y, 1);
            if (num > n)
            {
                n = n * 2;
                Array.Resize(ref y, n + 1);
                y[num] = v;
            }
            else
            {
                y[num] = v;
            }
        }
        //public static void setDynaArrVari(ref object y, int num, double v)
        //{
        //    int n;
        //    if (y == null)
        //    {
        //        y = new object[2];
        //        n = 2;
        //    }
        //    else
        //    {
        //        n = ((Array)y).Length;// Information.UBound((Array)y, 1);
        //    }
        //    if (num > n)
        //    {
        //        n *= 2;
        //        if (n < num)
        //        {
        //            n = num;
        //        }
        //        y = Utils.CopyArray((Array)y, new object[n + 1]);
        //        NewLateBinding.LateIndexSet(y, new object[] { num, v }, null);
        //    }
        //    else
        //    {
        //        NewLateBinding.LateIndexSet(y, new object[] { num, v }, null);
        //    }

        //    //int n = 0;
        //    //if (y == null)
        //    //{
        //    //    y = new double[10];

        //    //}
        //    //else
        //    //{
        //    //    n = Information.UBound(y, 1);
        //    //}

        //    //if (num > n)
        //    //{
        //    //    n = n * 2;
        //    //    if (n < num)
        //    //        n = num;
        //    //    Array.Resize<double>(ref y, n );
        //    //    y[num] = v;
        //    //}
        //    //else
        //    //{
        //    //    y[num] = v;
        //    //}
        //    ////todo
        //}

        public static double min(ref double[] x, int n)
        {
            int i = 0;
            double method1 = 0;
            method1 = x[0];
            for (i = 1; i <= n - 1; i++)
            {
                if (method1 > x[i])
                    method1 = x[i];
            }
            return method1;
        }
        public static double max(ref double[] x, int n)
        {
            int i = 0;
            double method1 = 0;
            method1 = x[0];
            for (i = 1; i <= n - 1; i++)
            {
                if (method1 < x[i])
                    method1 = x[i];
            }
            return method1;
        }
        public static double longmin(ref double[] x, int n)
        {
            int i = 0;
            double method1 = 0;
            method1 = x[0];
            for (i = 1; i <= n - 1; i++)
            {
                if (method1 > x[i])
                    method1 = x[i];
            }
            return method1;
        }
        public static double longmax(ref double[] x, int n)
        {
            int i = 0;
            double v1 = 0;
            v1 = x[0];
            for (i = 1; i <= n - 1; i++)
            {
                if (v1 < x[i])
                    v1 = x[i];
            }
            return v1;
        }
        public static void linear(ref double[] x, ref double[] y, int n, ref double k, ref double b)
        {
            int i = 0;
            double jxy = 0;
            double jx = 0;
            double jy = 0;
            double jx2 = 0;
            double zhi = 0;
            for (i = 0; i <= n - 1; i++)
            {
                jx = jx + x[i];
                jy = jy + y[i];
                jxy = jxy + x[i] * y[i];
                jx2 = jx2 + x[i] * x[i];
            }
            zhi = (jx2 * n - jx * jx);
            if (zhi != 0)
            {
                k = (jxy * n - jy * jx) / zhi;
                b = (jx2 * jy - jx * jxy) / zhi;
            }
        }

        public static double myLog(double a)
        {
            double functionReturnValue = 0;
            if (a > 0)
            {
                functionReturnValue = System.Math.Log(a);
            }
            return functionReturnValue;
        }
        public static double myDiv(double a, double b)
        {
            double functionReturnValue = 0;
            if (b != 0)
            {
                functionReturnValue = a / b;
            }
            return functionReturnValue;
        }

        public static double longminV(ref object[] x, ref int n)
        {
            int i = 0;
            double method1 = 0;
            method1 = (double)x[0];
            for (i = 1; i <= n - 1; i++)
            {
                if (method1 > (double)x[i])
                    method1 = (double)x[i];
            }
            return method1;
        }
        public static double longmaxV(ref object[] x, ref int n)
        {
            int i = 0;
            double R = 0;
            R = (double)x[0];
            for (i = 1; i <= n - 1; i++)
            {
                if (R < (double)x[i])
                    R = (double)x[i];
            }
            return R;
        }
        public static void savesignew(Sig mysig, int sid)
        {
            savesignew_ado(ref mysig, ref sid);
        }
        public static void savesignew_ado(ref Sig mysig, ref int sid)
        {
            try
            {
                mysig.save(sid);
                return;
            }
            catch (System.Exception e)
            {
                modLog.savelogmsg(e.ToString());
            }
        }
        public static void savesignew_db4o(ref Sig mysig, ref int sid)
        {
            //Db4objects.Db4o.IObjectContainer db = null;
            //db = Db4objects.Db4o.Db4oFactory.OpenFile(modLog.initpath + "\\volt.data");
            //mysig.sampleid = sid;
            //db.Set(mysig);
            //db.Close();
        }
        public static void loadsignew_1(ref Sig mysig, int sid)
        {
            //Db4objects.Db4o.IObjectContainer db = null;
            //db = Db4objects.Db4o.Db4oFactory.OpenFile(modLog.initpath + "\\volt.data");
            //Sig tmpsig = new Sig();
            //tmpsig.sampleid = sid;
            //Db4objects.Db4o.IObjectSet o = null;
            //o = db.Get(tmpsig);
            //if (o.Count > 0)
            //{
            //    mysig = (NCS_CS.Sig)o[0];
            //}
            //db.Close();
        }
        public static bool delsig(ref Sig mysig)
        {

            //Db4objects.Db4o.IObjectContainer db = null;
            //db = Db4objects.Db4o.Db4oFactory.OpenFile(modLog.initpath + "\\volt.data");
            //db.Delete(mysig);
            //db.Close();
            return true;
        }
        public static bool loadsignew(ref Sig mysig, int sid)
        {
            return mysig.load(sid);
                       //Db4objects.Db4o.IObjectContainer db = null;
            //db = Db4objects.Db4o.Db4oFactory.OpenFile(modLog.initpath + "\\volt.data");
            //Db4objects.Db4o.Query.IQuery query = db.Query();
            //query.Constrain(typeof(Sig));
            //query.Descend("sampleid").Constrain(sid);
            //Db4objects.Db4o.IObjectSet o = null;
            //o = query.Execute();
            //if (o.Count == 1)
            //{
            //    mysig = (NCS_CS.Sig)o[0];
            //}
            //else if (o.Count > 1)
            //{
            //    modLog.savelogmsg("load sig error >1");
            //    mysig = null;
            //}
            //else
            //{
            //    mysig = null;
            //}
            //db.Close();
            //if ((mysig != null))
            //{
            //    mysig.setstop();
            //    return true;
            //}
            //else
            //{
             //   return false;
            //}
        }
        //* Name       : BitSet                                      *
        //*----------------------------------------------------------*
        //* Purpose    : Sets a given Bit in Number                  *
        //*----------------------------------------------------------*
        public static byte BitSetChar(ref byte Number, byte Bit)
        {
            if (Bit == 7)
            {
                Number = (byte)(0x80 | Number);
            }
            else if (Bit < 7)
            {
                Number = (byte)((1 << Bit) | Number);
            }
            return Number;
        }


        //*----------------------------------------------------------*
        //* Name       : BitClear                                    *
        //*----------------------------------------------------------*
        //* Purpose    : Clears a given Bit in Number                *
        //*----------------------------------------------------------*
        public static byte BitClearChar(ref byte Number, byte Bit)
        {
            if (Bit == 7)
            {
                Number = (byte)(0x7f & Number);
            }
            else if (Bit < 7)
            {
                Number = (byte)(((1 << Bit) ^ 0xff) & Number);
            }

            return Number;
        }


        //*----------------------------------------------------------*
        //* Name       : BitIsSet                                    *
        //*----------------------------------------------------------*
        //* Purpose    : Test if bit 0 to bit 31 is set              *
        //*----------------------------------------------------------*
        public static bool BitIsSetChar(byte Number, byte Bit)
        {
            bool functionReturnValue = false;
            if (Bit == 7)
            {
                if ((Number & 0x80) > 0)
                    functionReturnValue = true;
            }
            else if (Bit < 7)
            {
                if ((Number & 1 << Bit) > 0)
                    functionReturnValue = true;
            }
            return functionReturnValue;
        }

        //public int inArr(ref int a, ref int[] yi, ref int num)
        //{
        //    int functionReturnValue =0;
        //    int i = 0;
        //    for (i = 0; i <= num - 1; i++) {
        //        if (yi[i] == a) {
        //            functionReturnValue = i;
        //            break; // TODO: might not be correct. Was : Exit For
        //        }
        //    }
        //    return functionReturnValue;
        //}
        public static int inArrNew(int a, int[] yi, int num, ref bool find)
        {
            int functionReturnValue = 0;
            find = false;
            int i = 0;
            for (i = 0; i <= num - 1; i++)
            {
                if (yi[i] == a)
                {
                    find = true;
                    functionReturnValue = i;
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
            return functionReturnValue;
        }

        public static int nextSampleId()
        {
            try
            {
                NCS.program P = ModMain.program1  ;
                int n = 0;
                n = P.sampleId;
                P.sampleId = n + 1;
                getsession().Update(P);
                getsession().Flush();
                return n + 1;
            }
            catch (System.Exception e)
            {
                modLog.savelogmsg(e.ToString());
                return -1;
            }
        }

        //public static void addDynaArrDistinct(ref int[] y, ref int n, int at, int v)
        //{
        //    if (inArrLong(ref v, ref y, ref n) == 0)
        //    {
        //        setDynaArrExat(ref y, ref n, at, ref v);
        //    }
        //}
        //public static int inArrLongN(ref int a, ref int[] name_var, ref int n)
        //{
        //    int functionReturnValue = 0;
        //    int i = 0;
        //    for (i = 0; i <= n - 1; i++)
        //    {
        //        if (a == name_var[i])
        //        {
        //            return i;
        //        }
        //    }
        //    return functionReturnValue;
        //}

        //public static void addDynaArrDistinctN(ref int[] y, ref int v, ref int at)
        //{
        //    //Dim n As Integer
        //    if (inArrLongN(ref v, ref y, ref at) == 0)
        //    {
        //        setDynaArrExat(ref y, ref at, at, ref v);
        //    }
        //}
        public static int inArrLong(ref int a, ref int[] name_var, ref int n)
        {
            int i = 0;
            for (i = 0; i <= n - 1; i++)
            {
                if (a == name_var[i])
                {
                    return i;
                }
            }
            return -1;
        }

        public static void setDynaArrExat(ref int[] y, ref int n, int i, ref int v)
        {
            if (i >= n)
            {
                n = i + 1;
                Array.Resize(ref y, n);
                y[i] = v;
            }
            else
            {
                y[i] = v;
            }
        }
        static void myconvert(string tmp, ref double a, ref double b0, ref double b1, ref double b2, ref double b3)
        {
            try
            {
                string[] s = tmp.Split('\t');
                a = Convert.ToDouble(s[0]);
                b0 = Convert.ToDouble(s[1]);
                b1 = Convert.ToDouble(s[2]);
                b2 = Convert.ToDouble(s[3]);
                b3 = Convert.ToDouble(s[4]);
            }
            catch (System.Exception e)
            {
                modLog.savelogmsg(e.ToString());
            }
        }
        public static bool LoadSigfromfile(ref string fname, ref Sig mysig, ref string n, ref double w)
        {
            //bool functionReturnValue = false;
            try
            {
                int c = 0;
                double a = 0;
                double b0 = 0;
                double b1 = 0;
                double b2 = 0;
                double b3 = 0;
                System.IO.FileStream f1 = new System.IO.FileStream(fname, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.StreamReader f = new System.IO.StreamReader(f1);
                n = f.ReadLine();
                string tmp = f.ReadLine();
                w = Convert.ToDouble(tmp);
                if (w != 0)
                {
                    while (!f.EndOfStream)
                    {
                        tmp = f.ReadLine();
                        myconvert(tmp, ref a, ref b0, ref b1, ref b2, ref b3);
                        mysig.setPoint(a, c);
                        mysig.setPoint(b0, "0", c);
                        mysig.setPoint(b1, "1", c);
                        mysig.setPoint(b2, "2", c);
                        mysig.setPoint(b3, "3", c);
                        c = c + 1;
                    }
                }
                f.Close();
                if (w != 0)
                {
                    mysig.xn = c;
                    mysig.setstop();
                    return true;
                }
            }
            catch (System.Exception e)
            {
                modLog.savelogmsg(e.ToString());
                return false;
            }
            return false;
        }

        public static double timePasted(double stime)
        {
            double res = 0;
            res = myTimer() - stime;
            return res;
        }
        public static void MySleep(int msecond)
        {
            System.Threading.Thread.Sleep(new System.TimeSpan(0, 0, 0, 0, msecond));
        }

        public static void KouJIxian(ref double[] x, ref double[] y, ref int n)
        {
            int smoothnum = 0;
            smoothnum = method1.smoothnum;
            double[] a = null;
            double[] b = null;
            int bcount = 0;
            int i = 0;
            double zerovalue = 0;
            //, mRand As Single
            if (n < 1)
                return;
            if (smoothnum > 1)
            {
                a = new double[n ];
                b = new double[n ];
                smooth3(ref x, ref y, n, 10, ref a, ref b, ref bcount);
                koujixian1(ref b, bcount);
                x = a;
                //(a)
                y = b;
                //(b)
                n = bcount;
            }
            else
            {
                //TRUE
                if (firstKoujixian)
                {
                    zerovalue = y[0];
                    for (i = 1; i <= n - 1; i++)
                    {
                        if (zerovalue > y[i])
                            zerovalue = y[i];
                    }
                    if (zerovalue == 0)
                    {
                        modLog.savelogmsg(Convert.ToString(i) + "in kou ji xian,zerovalue <=0");
                    }
                    for (i = 0; i <= n - 1; i++)
                    {
                        y[i] = Convert.ToSingle(y[i] - zerovalue + 0.0001);
                    }
                }
            }
        }
        static void koujixian1(ref double[] y, int n)
        {
            if (n < 1) return;
            long i;
            double y0;
            y0 = y[0];
            for (i = 1; i < n; i++)
            {
                if (y0 > y[i]) y0 = y[i];
            }
            for (i = 0; i < n; i++)
            {
                y[i] = y[i] - y0 + .0000001;
            }
        }
        static bool smooth3(ref double[] x, ref double[] y, int n, int bAN, ref double[] a, ref double[] b, ref int bcount)
        {
            if (n < bAN * 2 + 1)
            {
                return (false);
            }
            long i;
            bcount = 0;
            long j;
            for (i = bAN; i < n - bAN; i++)
            {
                a[bcount] = x[i];
                for (j = i - bAN; j < i + bAN + 1; j++)
                {
                    b[bcount] = b[bcount] + y[j];
                }
                b[bcount] = b[bcount] / (2 * bAN + 1);
                bcount = bcount + 1;
            }
            return true;
        }
        //        public static void KouJIxian(ref object x, ref object y, ref int n)
        //{
        //    short smoothnum = method1.smoothnum;
        //    if (n >= 1)
        //    {
        //        if (smoothnum > 1)
        //        {
        //            int bcount=0;
        //            double[] a = new double[n + 1];
        //            double[] b = new double[n + 1];
        //            smooth3(ref x, ref y, n, 10, ref a[0], ref b[0], ref bcount);
        //            koujixian1(ref b[0], bcount);
        //            x = a;
        //            y = b;
        //            n = bcount;
        //        }
        //        else if (true)
        //        {
        //            float zerovalue = Convert.ToSingle(NewLateBinding.LateIndexGet(y, new object[] { 0 }, null));
        //            //int VB$t_i4$L0 = n - 1;
        //            int i = 1;
        //            while (i <= n-1)
        //            {
        //                if (Operators.ConditionalCompareObjectGreater(zerovalue, NewLateBinding.LateIndexGet(y, new object[] { i }, null), false))
        //                {
        //                    zerovalue = Convert.ToSingle(NewLateBinding.LateIndexGet(y, new object[] { i }, null));
        //                }
        //                i++;
        //            }
        //            if (zerovalue == 0f)
        //            {
        //                modLog.savelogmsg(Convert.ToString(i) + "in kou ji xian,zerovalue <=0");
        //            }
        //            //int VB$t_i4$L1 = n - 1;
        //            for (i = 0; i <=n-1; i++)
        //            {
        //                NewLateBinding.LateIndexSet(y, new object[] { i, Convert.ToSingle(Operators.AddObject(Operators.SubtractObject(NewLateBinding.LateIndexGet(y, new object[] { i }, null), zerovalue), 0.0001)) }, null);
        //            }
        //        }
        //    }
        //}


        public static void generateArr(ref double[] Y1, ref int method1, ref double[] y, ref int n)
        {
            //have error ,不如generateArrnew
            int count = 0;
            int num = 0;
            //UPGRADE_NOTE: Sig was upgraded to Sig_Renamed. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
            double Sig_Renamed = 0;
            bool cont = false;
            cont = true;
            count = 0;
            //  Call setDynaArr(y1, count, y(0))
            setDynaArr(ref Y1, count, y[0]);
            method1 = count + 1;
            //debug.print y(count)
            do
            {
                num = num + 1;
                if (num > n - 1)
                {
                    method1 = count + 1; return;
                }
                Sig_Renamed = y[num] - y[num - 1];
            } while (Sig_Renamed == 0);
            while (cont)
            {
                while (true)
                {
                    num = num + 1;
                    if (num > n - 1)
                    {
                        cont = false; break; // TODO: might not be correct. Was : Exit Do
                    }
                    //debug.print num, count, y[num], y(num - 1), sig
                    if ((y[num] - y[num - 1]) * Sig_Renamed < 0)
                    {
                        count = count + 1;
                        setDynaArr(ref Y1, count, y[num - 1]);
                        Sig_Renamed = y[num] - y[num - 1];
                        break; // TODO: might not be correct. Was : Exit Do
                    }
                }
                //     Do
                //        Sig = y[num] - y(count): If Sig <> 0 Then Exit Do
                //        num = num + 1: If num > n - 1 Then method1 = count + 1: Exit Sub
                //     Loop
            }
            method1 = count + 1;
        }
        public static void generateArrNew(ref double[] Y1, ref int[] Index, ref int method1, ref double[] y, ref int n)
        {
            int count = 0;
            int num = 0;
            //UPGRADE_NOTE: Sig was upgraded to Sig_Renamed. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
            double Sig_Renamed = 0;
            bool cont = false;
            cont = true;
            count = 0;
            setDynaArr(ref Y1, count, y[0]);
            setDynaArrLong(ref Index, ref count, 0);
            method1 = count + 1;
            //debug.print y(count)
            do
            {
                num = num + 1;
                if (num > n - 1)
                {
                    method1 = count + 1; return;
                }
                Sig_Renamed = y[num] - y[num - 1];
            } while (Sig_Renamed == 0);
            while (cont)
            {
                while (true)
                {
                    num = num + 1;
                    if (num > n - 1)
                    {
                        cont = false; break; // TODO: might not be correct. Was : Exit Do
                    }
                    //'debug.print num, count, y[num], y(num - 1), sig
                    if ((y[num] - y[num - 1]) * Sig_Renamed < 0)
                    {
                        count = count + 1;
                        setDynaArr(ref Y1, count, y[num - 1]);
                        setDynaArrLong(ref Index, ref count, num - 1);
                        Sig_Renamed = y[num] - y[num - 1];
                        break; // TODO: might not be correct. Was : Exit Do
                    }
                }
                //     Do
                //        sig = y[num] - y(count): If sig <> 0 Then Exit Do
                //        num = num + 1: If num > N - 1 Then method1 = count + 1: Exit Sub
                //     Loop
            }
            method1 = count + 1;
        }
        public static double calcRandVariNew(ref double[] y, ref int n, ref int num)
        {
            //num how many feng
            double[] Y1 = null;
            //UPGRADE_WARNING: Couldn't resolve default property of object y. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            Y1 = y;
            return calcRand1New(ref Y1, ref n, ref num);
        }
        public static double calcRand1New(ref double[] y, ref int n, ref int num)
        {
            double functionReturnValue = 0;
            //平均峰高
            try
            {
                num = 0;
                int i = 0;
                double t = 0;
                int ynum = 0;
                ynum = 100;
                double[] Y1 = null;
                Y1 = new double[ynum + 1];
                int[] idx = null;
                idx = new int[ynum + 1];
                generateArrNew(ref Y1, ref idx, ref ynum, ref y, ref n);
                for (i = 1; i <= ynum - 1; i++)
                {
                    //debug.print y1[i], y1(i - 1)
                    t = t + System.Math.Abs(Y1[i] - Y1[i - 1]);
                }
                functionReturnValue = t / (ynum - 1);
                num = ynum;
                return functionReturnValue;
            }
            catch (System.Exception e)
            {
                modLog.savelogmsg(e.ToString());
            }
            return functionReturnValue;
        }

        public static double calcRandVari(ref double[] y, ref int n, ref int num)
        {
            //num how many feng
            double[] Y1 = null;
            //UPGRADE_WARNING: Couldn't resolve default property of object y. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            Y1 = y;
            return calcRand1(ref Y1, ref n, ref num);
        }
        public static double calcRand1(ref double[] y, ref int n, ref int num)
        {
            double functionReturnValue = 0;
            //平均峰高
            try
            {
                num = 0;
                int i = 0;
                double t = 0;
                int ynum = 0;
                ynum = 100;
                double[] Y1 = null;
                Y1 = new double[ynum + 1];
                int[] idx = null;
                idx = new int[ynum + 1];
                generateArr(ref Y1, ref ynum, ref y, ref n);
                if (ynum < 1)
                    return 0;
                t = System.Math.Abs(Y1[1] - Y1[0]);
                for (i = 2; i <= ynum - 1; i++)
                {
                    //debug.print y1[i], y1(i - 1)
                    if (t < System.Math.Abs(Y1[i] - Y1[i - 1]))
                        t = System.Math.Abs(Y1[i] - Y1[i - 1]);
                }
                functionReturnValue = t;
                num = ynum;
                return t;
            }
            catch (System.Exception e)
            {
                modLog.savelogmsg(e.ToString());
            }
            return functionReturnValue;
        }
        public static void setDynaArrLong(ref int[] y, ref int num, int v)
        {
            int n = 0;
            //n = Information.UBound(y, 1);
            //n=System.Array.
            n = y.Length;
            if (num > n)
            {
                n = n * 2;
                Array.Resize(ref y, n + 1);
                y[num] = v;
            }
            else
            {
                y[num] = v;
            }
        }



        public static bool checkOne(ref double[] y, ref int n, ref double zerovalue, ref double mRand)
        {
            bool passCheck = false;
            passCheck = true;
            int i = 0;
            int nRand = 0;
            nRand = n;
            //    If n < nRand Then Exit Function
            for (i = 1; i <= nRand; i++)
            {
                //UPGRADE_WARNING: Couldn't resolve default property of object y(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                zerovalue = zerovalue + y[i];
                //      mRand = mRand + Abs(y[i] - y(i - 1))
            }
            //    mRand = 3 * mRand / nRand * 4 'error calc rand
            int newNum = 0;
            mRand = calcRandVari(ref y, ref nRand, ref newNum);
            zerovalue = zerovalue / nRand;
            //'
            double miny = 0;
            double maxy = 0;
            miny = y[0];
            maxy = y[0];
            for (i = 1; i <= n - 1; i++)
            {
                if (miny < y[i])
                {
                    miny = y[i];
                }
                else if (maxy > y[i])
                {
                    maxy = y[i];
                }
            }
            //
            if (miny == 0 | miny > 1)
            {
                modLog.savelogmsg("基线不在合适范围,>0 <0.05");
                passCheck = false;
            }
            //
            if (maxy - miny > mRand)
            {
                modLog.savelogmsg("基线不稳,波动为" + Convert.ToString(maxy - miny) + ">" + Convert.ToString(mRand));
                passCheck = false;
            }
            return passCheck;
        }

        public static void showStandard()
        {
            FrmStandard s = new FrmStandard();
            s.ShowDialog();
        }
        //平滑
        public static void smooth2(ref double[] a, ref int n, int ban)
        {
            if (n < ban * 2 + 1)
            {
                modLog.savelogmsg("error smooth,no enoungh points");
                return;
            }
            int i = 0;
            int j = 0;
            double[] b = null;
            b = new double[n + 1];
            for (i = 0; i <= ban - 1; i++)
            {
                for (j = 0; j <= i; j++)
                {
                    //UPGRADE_WARNING: Couldn't resolve default property of object a[j]. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b[i] = b[i] + a[j];
                }
                b[i] = b[i] / (i + 1);
            }
            for (i = ban; i <= n - ban - 1; i++)
            {
                for (j = -ban; j <= ban; j++)
                {
                    //UPGRADE_WARNING: Couldn't resolve default property of object a(i + j). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b[i] = b[i] + a[i + j];
                }
                b[i] = b[i] / (2 * ban + 1);
            }
            for (i = n - ban; i <= n - 1; i++)
            {
                for (j = i; j <= n - 1; j++)
                {
                    //UPGRADE_WARNING: Couldn't resolve default property of object a[j]. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b[i] = b[i] + a[j];
                }
                b[i] = b[i] / (n - i);
            }
            for (i = 0; i <= n - 1; i++)
            {
                //UPGRADE_WARNING: Couldn't resolve default property of object a(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                a[i] = b[i];
            }
        }
        public static double myTimer()
        {
            return timeGetTime() / 1000.0F;
        }

        //平滑
        public static void average05(ref double[] x, ref double[] y, ref int n)
        {
            double ban = 0;
            ban = 0.5F;
            int i = 0;
            //Dim btime As Single
            double[] b = null;
            b = new double[n + 1];
            double[] a = null;
            a = new double[n + 1];
            int bcount = 0;
            //UPGRADE_WARNING: Couldn't resolve default property of object x(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            a[bcount] = x[0];
            //btime = x(0)
            int num = 0;
            while (i < n)
            {
                //UPGRADE_WARNING: Couldn't resolve default property of object x(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                a[bcount] = x[i];
                num = 0;
                //UPGRADE_WARNING: Couldn't resolve default property of object x[i]. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                while (i < n & x[i] - a[bcount] < ban)
                {
                    //UPGRADE_WARNING: Couldn't resolve default property of object y[i]. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b[bcount] = b[bcount] + y[i];
                    i = i + 1;
                    num = num + 1;
                }
                b[bcount] = b[bcount] / num;
                bcount = bcount + 1;

            }
            //UPGRADE_WARNING: Couldn't resolve default property of object x. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            x = a;
            //(a)
            //UPGRADE_WARNING: Couldn't resolve default property of object y. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            y = b;
            //(b)
            n = bcount;
            //Debug.Print("")
            //For i = 0 To n - 1
            //    Debug.Print(VB6.TabLayout(x[i], y[i]))
            //Next i
            //For i = ban To n - ban - 1
            //  For j = -ban To ban
            //  b[i] = b[i] + a(i + j)
            //  Next j
            //  b[i] = b[i] / (2 * ban + 1)
            //Next i
            //For i = n - ban To n - 1
            //  For j = i To n - 1
            //    b[i] = b[i] + a[j]
            //  Next j
            //  b[i] = b[i] / (n - i]
            //Next i
            //For i = 0 To n - 1
            //   a[i] = b[i]
            //Next i
        }

        //平滑
        public static void smooth(ref double[] x, ref double[] y, ref int n, ref int ban)
        {
            if (n < 2 * ban + 1)
                return;
            //Dim bAN As Integer
            //bAN = 0.5
            int i = 0;
            //Dim btime As Single
            double[] b = null;
            b = new double[n + 1];
            double[] a = null;
            a = new double[n + 1];
            int bcount = 0;
            //a(bcount) = x(0)
            //Dim num As int
            int j = 0;
            for (i = ban; i <= n - ban - 1; i++)
            {
                //UPGRADE_WARNING: Couldn't resolve default property of object x(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                a[bcount] = x[i];
                for (j = i - ban; j <= i + ban; j++)
                {
                    //UPGRADE_WARNING: Couldn't resolve default property of object y[j]. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                    b[bcount] = b[bcount] + y[j];
                }
                b[bcount] = b[bcount] / (2 * ban + 1);
                bcount = bcount + 1;
            }
            //Debug.Print n, bcount, bAN
            //Debug.Print a(bcount - 1), b(bcount - 1)
            //UPGRADE_WARNING: Couldn't resolve default property of object x. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            x = (a);
            //UPGRADE_WARNING: Couldn't resolve default property of object y. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            y = (b);
            n = bcount;
            //Debug.Print
            //For i = 0 To n - 1
            //    Debug.Print x[i], y[i]
            //Next i
        }

        public static bool checkuser()
        {
            bool functionReturnValue = false;
            string v = null;
            if (user1.mname == "system")
            {
                functionReturnValue = true;
            }
            else
            {
                frmInputPWD f = new frmInputPWD();
                f.ShowDialog();
                //v = InputBox("管理员密码", "输入", "")
                v = f.txtPassword.Text;
                //If frmLogin2.LoginSucceeded Then
                if (f.LoginSucceeded)
                {
                    if (v == gDb.getsystemPwd())
                    {
                        functionReturnValue = true;
                        return functionReturnValue;
                    }
                }
                functionReturnValue = false;
                modLog.savelogmsg("只有system用户可以进入");
                modLog.showLog();
            }
            return functionReturnValue;
        }
        //public static System.Data.OleDb.OleDbConnection getCon()
        //{
        //    System.Data.OleDb.OleDbConnection con = null;
        //    string constr = null;
        //    con = new System.Data.OleDb.OleDbConnection();
        //    constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + modLog.initpath + "\\data.mdb;Persist Security Info=False";
        //    con.ConnectionString = constr;
        //    // "FILE NAME=" + initPATH + "\data.udl"
        //    con.Open();
        //    return con;
        //}

        public static int getEleChanel(string vele)
        {
            return gchanelDic[vele].chanelno ;
        }
        public static NCS.curveXishu getChanelxishu(string vele)
        {

            ISession s = ModMain.getsession();
            //s.
            foreach (NCS.curveXishu c in method1.curveXishuList)
            {
                //c = c_loopVariable;
                if (c.ele == vele)
                {
                    return c;
                    //break; // TODO: might not be correct. Was : Exit For

                }
            }
            
            return null;
        }
        public static bool getChanelEnable(string vele)
        {
            foreach (NCS.chanel c in gchanel)
            {
                if (c.ele == vele)
                {
                    return c.enable;
                }
            }
            return false;
        }
        public static Color  getChanelColor(string vele)
        {
            foreach (NCS.chanel c in gchanel)
            {
                if (c.ele == vele)
                {
                    return Color.FromArgb(c.color) ;
                }
            }
            return Color.FromArgb(0);
        }
        public static bool getChanelEnable(int i)
        {
            switch (i)
            {
                case 0:
                    return getChanelEnable("C");
                case 1:
                    return getChanelEnable("S");
                case 2:
                    return getChanelEnable("C2");
                case 3:
                    return getChanelEnable("S2");
                default:
                    return false;
            }
        }
    }

}
